<?php

namespace App\Repositories;


use App\Interfaces\CategoryService;
use App\Models\Category;
use App\Models\CategoryAttribute;
use App\Models\CategoryAttributeValue;
use Illuminate\Database\Eloquent\Collection;

class CategoryRepository implements CategoryService{

    protected $category;

    public function __construct(){

    }

    /**
     * 获取单个分类
     * @param $id
     * @return mixed
     */
    public function get($id){

        return Category::find($id);
    }

    /**
     *
     * 获取商品分类树
     * @return mixed
     */
    public function getTree(){

        return Category::getTree();
    }

    /**
     * 获取顶级分类
     * @return mixed
     */
    public function top(){
//dd(Category::where('parent_id',0)->orderBy('sort_order','desc')->orderBy('cat_id','asc')->get());
        return Category::where('parent_id',0)->orderBy('sort_order','desc')->orderBy('cat_id','asc')->get();

    }

    /**
     * 获取子分类节点
     * @param $id
     * @return mixed
     */
    public function getChild($id){
        $category = Category::find($id);
        if ($category){
            return $category->getChildren();
        }
    }

    public function getChildIds($id){
        $category = Category::find($id);
        if ($category){
            return $category->getChildIds();
        }else{
            return collection();
        }
    }

    public function getBrotherIds($id){
        $category = Category::find($id);
        if ($category){
            return $this->getChildIds($category->parent_id);
        }else{
            return collection();
        }
    }

    public function getDepth($cat_id){
        $category = Category::find($cat_id);

        if ($category){
            return $category->getDepth();
        }else{
            return collection();
        }
    }

    /**
     * 获取父分类
     * @param $id
     * @return mixed
     */
    public function getParent($id){
//TODO 该方法有待完善
        //$category = $this->category->find($id);
        $category = Category::find($id);

        if ($category){
            //return $this->category->where('parent_id',$category->parent_id)->orderBy('sort_order','desc')->orderBy('cat_id','asc')->get();

            return Category::where('cat_id',$category->parent_id)->orderBy('sort_order','desc')->orderBy('cat_id','asc')->get();
        }//end if()

    }

    public function getPath($id){
        $path = new Collection();
        $category = Category::find($id);
        if ($category) {
            $array = array_reverse($category->getPath());
            $path = $path->merge($array);
            $path->push($category);

        }else{
            $category = new Collection();
        }

        return $path;
    }



    public function getCatByIds(Array $cat_ids){
        return Category::whereIn('cat_id',$cat_ids)->get();
    }

    public function getCategoryAttribute($cat_id){
        $categoryAttribute = CategoryAttribute::where('cat_id',$cat_id)->orderBy('sort_order','desc')->get();
        $categoryAttributeArr=array();
        foreach ($categoryAttribute as $key=>$value) {
            $categoryAttributeArr[$key]['id']=$value->id;
            $categoryAttributeArr[$key]['attribute_name']=$value->attribute_name;
            $categoryAttributeArr[$key]['attribute_value'] = CategoryAttributeValue::where('category_attribute_id',$value->id)->orderBy('id','desc')->get()->toArray();
        }
        return $categoryAttributeArr;
    }



    /**
     * 根据分类id获取分类
     * @param $cat_id
     * @return mixed
     */
    public function getByCatId($cat_id){
        return Category::find($cat_id);
    }

}